package com.pz.operation01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Normal1 {
    /**
     * 找出一个整数数组中子数组之和的最大值
     *
     * [1 -2 3 5 -1] -->[3 5]
     * [1 -2 3 -8 5 1] -->[5 1]
     * [1 -2 3 -2 5 1] -->[3 -2 5 1]
     *[-2 1 -3 4 -1 2 1 -5 4] -->[4 -1 2 1]
     * **/
    public static void work1_1(){
        System.out.println("请输入数组：");
        Scanner scanf = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int sum=0;  //全局最大总和
        String[] temps = scanf.nextLine().split(" ");
        for(String temp : temps){
            sum+=Integer.parseInt(temp);
            list.add(Integer.parseInt(temp));
        }

        int index_head, index_tail; //定义首尾标识
        int final_heda = 0,final_tail = list.size()-1;  //最优解的定位

        for(index_head = 0; index_head < list.size(); index_head++){
            for(index_tail =index_head; index_tail < list.size(); index_tail++){
                int temp_sum = 0;   //暂存局部最大总和
                for(int i = index_head; i <= index_tail; i++){
                    temp_sum+=list.get(i);
                }
                if(temp_sum>sum){
                    sum = temp_sum;
                    final_heda=index_head;
                    final_tail=index_tail;
                }
            }
        }

        System.out.println(sum);
        System.out.println("head："+  final_heda);
        System.out.println("tail："+final_tail);
        System.out.println("最大和子数组为："+list.subList(final_heda,final_tail+1));
    }
    public static int maxSubArray(int[] nums) {  //此为算法消耗最小的优解
        int max = nums[0];
        int temp = 0;
        for(int num : nums){
            if(temp > 0){
                temp += num;
            }else{
                temp = num;
            }
            max = Math.max(max,temp);
        }
        return max;
    }


    /**
     * *判断素数
     * */
    public static boolean isPrime(int n){
        if(n < 2) return false;
        if(n == 2) return true;
        if(n%2==0) return false;
        for(int i = 3; i*i <= n; i+=2){
            if(n%i == 0) return false;
        }
        return true;
    }
    //输入1~20000内的所有素数，按每行5个打印出来
    public static void showPrime(int n){
        int flag = 0;
        for (int i = 2; i <= n; i++){
            if(isPrime(i) == true){
                flag++;
                System.out.print(i+" ");
            }
            if(flag == 5){
                System.out.println();
                flag = 0;
            }
        }
    }


    public static void test(){
        work1_1();
        System.out.println("继续（y）,退出（n）");
        Scanner scanf = new Scanner(System.in);
        String choice = scanf.next();
        while (choice.equals("y")){
            work1_1();
            System.out.println("继续（y）,退出（其他）");
            scanf = new Scanner(System.in);
            choice = scanf.next();

        }
    }


    /**
     * GUI鼠标跟随,按下鼠标文字就会到鼠标的位置
     * **/
    public static void GUI_mouseFellow(){
        JFrame f = new JFrame("鼠标跟随");
        f.setSize(400, 300);
        f.setLocation(200, 200);
        f.setLayout(null);
        JLabel l = new JLabel("hello world!");
        //文字颜色
        l.setForeground(Color.red);
        l.setBounds(50, 50, 280, 30);

        f.add(l);

        f.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                l.setLocation(e.getX(),e.getY());
            }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        System.out.println("Hello World!!!");
        //test();
//        int nums[] = {1, -2, 3, 5, -1};
        //System.out.println(isPrime(3));
        //showPrime(20000);
        GUI_mouseFellow();
    }
}
